
#include "rms_norm_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

//UB:253952

namespace optiling {
    const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

    RmsNormTilingData tiling;

    uint32_t x1_size = context->GetInputShape(0)->GetStorageShape().GetShapeSize(); //输入数量

    uint32_t x2_size = context->GetInputShape(1)->GetStorageShape().GetShapeSize(); //输入数量
    uint32_t x2_bytes = GetSizeByDataType(context->GetInputDesc(1)->GetDataType()); //输入类型
    // uint32_t x2_length = x2_bytes * x2_size; //输入长度

    float epsilon = *context->GetAttrs()->GetFloat(0);

    tiling.set_epsilon(epsilon);

    tiling.set_loop_num(x1_size/x2_size);
    tiling.set_x2_size(x2_size);

    auto dt = context->GetInputTensor(1)->GetDataType();
    
    
    if((x2_size * x2_bytes) % BLOCK_SIZE == 0)
    {
        uint32_t ubDataNumber = 3*2+4;
        if (dt == ge::DT_FLOAT) ubDataNumber = 3*2;
        else ubDataNumber = 3*2 + 4;

        uint64_t ubSize;
        auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
        ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小
        // printf("usb:%ld ub",ubSize);
        ubSize = ubSize - 4*64;
        uint32_t tileBlockNum = (ubSize / BLOCK_SIZE) / ubDataNumber; //每个ub段可用的空间块数
        uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / x2_bytes; //每次处理的数据量
        uint32_t TileNum = x2_size / tileDataNum;
        uint32_t finalTileNum = (x2_size % tileDataNum) == 0 ? TileNum : TileNum + 1; //需要循环处理几次
        uint32_t TailDataNum = x2_size - (tileDataNum * TileNum);
        TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum; //最后一次需要处理的数据量

        // printf("!%d!",tileDataNum);

        tiling.set_finalTileNum(finalTileNum);//需要循环处理几次
        tiling.set_tileDataNum(tileDataNum); //每次处理的数据量
        tiling.set_TailDataNum(TailDataNum); //最后一次需要处理的数据量

        context->SetTilingKey(1);
    }
    else
    {
        uint32_t ubDataNumber = 3*2+4;
        if (dt == ge::DT_FLOAT) ubDataNumber = 3*2 + 2;
        else ubDataNumber = 3*2 + 4;

        uint64_t ubSize;
        auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
        ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小
        // printf("usb:%ld ub",ubSize);
        ubSize = ubSize - 4*64;
        uint32_t tileBlockNum = (ubSize / BLOCK_SIZE) / ubDataNumber; //每个ub段可用的空间块数
        uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / x2_bytes; //每次处理的数据量
        uint32_t TileNum = x2_size / tileDataNum;
        uint32_t finalTileNum = (x2_size % tileDataNum) == 0 ? TileNum : TileNum + 1; //需要循环处理几次
        uint32_t TailDataNum = x2_size - (tileDataNum * TileNum);
        TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum; //最后一次需要处理的数据量

        tiling.set_finalTileNum(finalTileNum);//需要循环处理几次
        tiling.set_tileDataNum(tileDataNum); //每次处理的数据量
        tiling.set_TailDataNum(TailDataNum); //最后一次需要处理的数据量

        context->SetTilingKey(2);
    }
 
  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class RmsNorm : public OpDef {
public:
    explicit RmsNorm(const char* name) : OpDef(name)
    {
        this->Input("x1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("rstd")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("epsilon").AttrType(OPTIONAL).Float(1e-06);

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(RmsNorm);
}
